package main.gui.workspace;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Properties;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import main.Core;
import main.gui.dataobjecteditor.DataObjectEditor;

import character.sprite.gui.CharacterSpriteBuilder;

public class Workspace implements ActionListener, WindowListener {

	private static final long serialVersionUID = -4569447018025061640L;

	private static Workspace instance = null;

	private static Properties workspaceProperties;

	private String workspacePropsPath;
	private JFrame frame;
	private JButton sprite_builder_button;

	/**
	 * Default constructor.
	 */
	private Workspace() {
		// Set look and feel to default system look and feel
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}

		// Load the properties for the workspace
		// Workspace.workspaceProperties = new Properties();
		// workspacePropsPath = Paths.propertiesPath
		// + File.separator
		// + "workspace.properties";
		// try {
		// File workspacePropsFile = new File(workspacePropsPath);
		// FileInputStream fis = new FileInputStream(workspacePropsFile);
		// Workspace.workspaceProperties.load(fis);
		// } catch (FileNotFoundException e) {
		// Core.getInstance().logMessage(
		// Workspace.class.getName(),
		// "Workspace properties file could not be found at "
		// + workspacePropsPath, e);
		// } catch (IOException e) {
		// Core.getInstance().logMessage(
		// Workspace.class.getName(),
		// "Error loading workspace properties from "
		// + workspacePropsPath, e);
		// }
	}

	/**
	 * Singleton accessor.
	 */
	public static Workspace getInstance() {
		if (instance == null) {
			instance = new Workspace();
		}
		return instance;
	}

	/**
	 * Sets up and displays the frame.
	 */
	public void display() {
		// Set title
		frame = new JFrame("RPG Architect");
		// Set workspace frame properties
		frame.setSize(1024, 768);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setIconImage(new ImageIcon("SwordShieldIcon.png").getImage());
		// Setup GUI elements
		JButton spriteBuildingButton = new JButton("Sprite Builder");
		spriteBuildingButton.addActionListener(this);
		frame.setLayout(new GridLayout(2, 1));
		frame.add(spriteBuildingButton);
		frame.addWindowListener(this);
		// Set visible
		frame.setVisible(true);
	}

	public void openDataObjectEditor(DataObjectEditor editor) {
		JScrollPane scrollPane = new JScrollPane(editor);
		scrollPane
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		scrollPane
				.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		JDialog editorDialog = new JDialog(frame, "Editor Test", true);
		editorDialog.setSize(600, 768);
		editorDialog.setResizable(false);
		editorDialog.setLayout(new BorderLayout());
		editorDialog.add(scrollPane, BorderLayout.CENTER);
		editorDialog.setVisible(true);
	}

	/* Action Listener Methods */

	@Override
	public void actionPerformed(ActionEvent arg0) {
		JButton source = (JButton) arg0.getSource();
		if (source.equals(sprite_builder_button)) {
			// Character sprite edit button pressed
			CharacterSpriteBuilder builder = new CharacterSpriteBuilder(frame,
					null);
			builder.setVisible(true);
		}
	}

	/* Window Listener Methods */

	@Override
	public void windowClosing(WindowEvent arg0) {
		// Shutdown core
		Core.getInstance().shutdown();

		// Save properties
		// Workspace.workspaceProperties.setProperty("maximized",
		// ((Boolean) (frame.getExtendedState() == 6)).toString());
		// try {
		// File workspacePropsFile = new File(workspacePropsPath);
		// FileOutputStream fos = new FileOutputStream(workspacePropsFile);
		// Workspace.workspaceProperties.store(fos,
		// "Stored workspace properties for date " + );
		// } catch (FileNotFoundException e) {
		// Core.getInstance().logMessage(
		// Workspace.class.getName(),
		// "Workspace properties file could not be found at "
		// + workspacePropsPath, e);
		// } catch (IOException e) {
		// Core.getInstance().logMessage(
		// Workspace.class.getName(),
		// "Error loading workspace properties from "
		// + workspacePropsPath, e);
		// }

		// Dispose frame
		frame.dispose();
	}

	@Override
	public void windowActivated(WindowEvent arg0) {
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
	}

}
